home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 03 Pathfinding with Astar / 01 Matthews / ase / PathFinder.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  1.9 KB  |  70 lines

  1. //////////////////////////////////////////////////////////////////
  2. // Class:    CAStar class (27/6/2001)
  3. // File:    AStar.h
  4. // Author:    James Matthews
  5. //
  6. // Implements the A* algorithm.
  7. // 
  8. //
  9. // Please visit http://www.generation5.org/ for the latest
  10. // in Artificial Intelligence news, interviews, articles and
  11. // discussion forums.
  12. //
  13.  
  14. #ifndef _ASTAR_H_
  15. #define _ASTAR_H_
  16.  
  17. #include <memory.h>
  18. #include "AsIncludes.h"
  19.  
  20. class CAStar {
  21.     public:
  22.         CAStar();
  23.         ~CAStar();
  24.  
  25.         _asFunc     udCost;            // Called when cost value is need
  26.         _asFunc  udValid;            // Called to check validity of a coordinate
  27.         _asFunc  udNotifyChild;        // Called when child is added/checked (LinkChild)
  28.         _asFunc     udNotifyList;        // Called when node is added to Open/Closed list
  29.  
  30.         void    *m_pCBData;            // Data passed back to callback functions
  31.         void    *m_pNCData;            // Data passed back to notify child functions
  32.  
  33.         bool    GeneratePath(int, int, int, int);
  34.         int        Step();
  35.         void    StepInitialize(int, int, int, int);
  36.         void    SetRows(int r)         { m_iRows = r;    }
  37.         void    Reset() { m_pBest = NULL; }
  38.  
  39.         _asNode    *GetBestNode() { return m_pBest; }
  40.  
  41.     protected:
  42.         int        m_iRows;            // Used to calculate node->number
  43.         int        m_iSX, m_iSY, m_iDX, m_iDY, m_iDNum;
  44.  
  45.         _asNode    *m_pOpen;            // The open list
  46.         _asNode    *m_pClosed;            // The closed list
  47.         _asNode *m_pBest;            // The best node
  48.         _asStack*m_pStack;            // Propagation stack
  49.  
  50.         // Functions.
  51.         void    AddToOpen(_asNode *);
  52.         void    ClearNodes();
  53.         void    CreateChildren(_asNode *);
  54.         void    LinkChild(_asNode *, _asNode *);
  55.         void    UpdateParents(_asNode *);
  56.  
  57.         // Stack Functions.
  58.         void    Push(_asNode *);
  59.         _asNode *Pop();
  60.         
  61.         _asNode *CheckList(_asNode *, int);
  62.         _asNode    *GetBest();
  63.         
  64.         // Inline functions.
  65.         inline int Coord2Num(int x, int y) { return x * m_iRows + y; }
  66.         inline int udFunc(_asFunc, _asNode *, _asNode *, int, void *);
  67. };
  68.  
  69. #endif
  70.